package group.igfd.manager.biz.utils;

import org.apache.commons.lang.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

public class CommonConstant {

    public static final String SOURCES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";

    public static double CONVERSION_RATE = 1.01D;

    public static final String AES_CONNECTOR_STRING = "#*@";

    /**
     * token请求头名称
     */
    String REQ_HEADER = "Authorization";

    /**
     * token分割符
     */
    String TOKEN_SPLIT = "Bearer ";

    /**
     * jwt签名
     */
    String SIGN_KEY = "igfd";
    /**
     * 删除
     */
    String STATUS_DEL = "1";
    /**
     * 正常
     */
    String STATUS_NORMAL = "0";

    /**
     * 锁定
     */
    String STATUS_LOCK = "9";

    /**
     * 菜单
     */
    String MENU = "0";

    /**
     * 按钮
     */
    String BUTTON = "1";

    /**
     * 删除标记
     */
    String DEL_FLAG = "del_flag";

    /**
     * 编码
     */
    String UTF8 = "UTF-8";

    /**
     * JSON 资源
     */
    String CONTENT_TYPE = "application/json; charset=utf-8";

    /**
     * 阿里大鱼
     */
    String ALIYUN_SMS = "aliyun_sms";

    /**
     * 路由信息Redis保存的key
     */
    String ROUTE_KEY = "_ROUTE_KEY";

    /**
     * 短信验证码前缀
     */
    public static String REDIS_SMS_PRE = "SMS:";

    /**
     * TOKEN前缀
     */
    public static String REDIS_TOKEN_PRE = "Authorization:";

    /**
     * 微信Oauth2Token前缀
     * 2小时有效期
     */
    public static String REDIS_Oauth2Token_PRE = "WeChat:Oauth2Token:";
    /**
     * 微信Oauth2RefreshToken前缀
     * 30天有效期
     */
    public static String REDIS_Oauth2RefreshToken_PRE = "WeChat:Oauth2RefreshToken:";
    /**
     * 微信全局GlobalAccessToken
     * 2小时有效期
     */
    public static String REDIS_GlobalAccessToken_PRE="WeChat:GlobalAccessToken";
    /**
     * 微信jsapi_ticket前缀
     * 2小时有效期
     */
    public static String REDIS_JSPAI_Ticket_PRE = "WeChat:JsapiTicket";
    /**
     * 微信JS-SDK签名前缀
     * 30天有效期
     */
    public static String REDIS_JS_SDK_PRE = "WeChat:Signatur:";
    /**
     * 用户UnionId；
     */
    public static String REDIS_UnionId_PRE="WeChat:UnionId:";

    /**
     * 订单失效前缀
     */
    public static String REDIS_OrderNO_PRE="OrderNo:";

    /**
     * 短信验证码失效时间
     */
    public static Integer SMS_CODE_MISS_TIME = 5;

    /**
     * 手机号码校验规则
     */
    public static String PHONE_REGEXP = "^[1][0-9]{10}$";

    /**
     * 注册方式
     */
    public static Integer PLATFORM_NORMAL = 1;//普通注册
    public static Integer PLATFORM_WECAT = 2;//微信注册

    /**
     * 用户角色
     */
    public static Integer ROLE_CUSTOMER = 1;//客人身份
    public static Integer ROLE_BUYER = 2;//买手身份
    public static Integer ROLE_EXPERT = 3;//达人身份

    /**
     * 用户注册后默认的头像
     */
    public static String USER_NORMAL_AVATAR = "http://img.igfd.group/img/default_avatar.jpg?x-oss-process=style/def";//"http://beebuy.igfd.group/assets/default_avatar.png";
    //http://img.igfd.group/img/default_avatar.png?x-oss-process=style/def
    //http://img.igfd.group/img/10600434474617610253863561613775834306.png?x-oss-process=style/def
    /**
     * 上传图片URL前缀
     */
    public static String ALIYUN_IMAGEURL_PRE = "http://img.igfd.group";
    public static String ALIYUN_IMAGEURL_SUFF = "?x-oss-process=style/def";
    public static String ALIYUN_IMAGEURL_PRE_STYLE = "http://img-invo.igfd.group";
    public static String ALIYUN_IMAGEURL_SUFF_STYLE = "?x-oss-process=style/invo";

    public static String ALIYUN_IMAGEURL_SUFF_STYLE_500 = "?x-oss-process=style/invo_500";
    public static String ALIYUN_IMAGEURL_SUFF_STYLE_1000 = "?x-oss-process=style/invo_1000";
    public static String ALIYUN_IMAGEURL_SUFF_STYLE_1600 = "?x-oss-process=style/invo_1600";
    public static String ALIYUN_IMAGEURL_SUFF_STYLE_3000 = "?x-oss-process=style/invo_3000";
    public static String ALIYUN_IMAGEURL_SUFF_STYLE_6000 = "?x-oss-process=style/invo_6000";
    public static String ALIYUN_IMAGEURL_SUFF_STYLE_8000 = "?x-oss-process=style/invo_8000";

    /**
     * 定义订单状态的
     */
    public static Integer ORDER_STATUS_0 = 0; //下单 -- 待付款
    public static Integer ORDER_STATUS_1 = 1; //填写完收货地址和密语 -- 待付款
    public static Integer ORDER_STATUS_2 = 2; //已付款
    public static Integer ORDER_STATUS_3 = 3; //原状态保留 -- 等待买手完善信息
    public static Integer ORDER_STATUS_4 = 9; //已上传购买凭证 -- 待发货
    public static Integer ORDER_STATUS_5 = 10;//已上传打包信息 -- 待发货
    public static Integer ORDER_STATUS_6 = 4; //已发货 -- 待客人确认收货
    public static Integer ORDER_STATUS_7 = 5; //已确认收货 -- 待评价
    public static Integer ORDER_STATUS_8 = 6; //买手已评价，客人没有评价
    public static Integer ORDER_STATUS_9 = 7; //客人评价，买手没有评价
    public static Integer ORDER_STATUS_10 = 8;//订单完成
    public static Integer ORDER_STATUS_11 = 11;//交易关闭
    public static Integer ORDER_STATUS_12 = -1;//客人取消订单

    /**
     * 下单标记
     */
    public static Integer CUSTOMER_GOOD_ORDER = 0;
    public static Integer BUYER_GOOD_ORDER = 1;

    /**
     * 生成订单号
     *
     * @return
     */
    public static String createOrderNo() {
        String randomStr = generateString(new Random(), SOURCES, 5);
        return System.currentTimeMillis() + randomStr;
    }

    public static String generateString(Random random, String characters, int length) {
        char[] text = new char[length];
        for (int i = 0; i < length; i++) {
            text[i] = characters.charAt(random.nextInt(characters.length()));
        }
        return new String(text);
    }

    /**
     * 生成uuid
     *
     * @return
     */
    public static String createUUid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static String createNumberStr(int number) {
        String result = "";

        if (number < 1000) {
            result = result + number;
        } else if (number >= 1000 && number < 9999) {
            double c = number / 1000.0;
            result = String.format("%.1f", c) + "k";
        } else if (number >= 10000) {
            double c = number / 10000.0;
            result = String.format("%.1f", c) + "w";
        }

        return result;
    }

    /**
     * 时间转换，例如：5分钟前，3小时前，9月20号
     *
     * @param time
     * @return
     */
    public static String timeConvertString(Date time) {

        long preTime = time.getTime();
        long currentTime = new Date().getTime();

        int minutes = (int) ((currentTime - preTime) / (1000 * 60));
        int hours = (int) ((currentTime - preTime) / (1000 * 60 * 60));
        int days = (int) ((currentTime - preTime) / (1000 * 60 * 60 * 24));

        if (minutes > 0 && minutes < 60) {
            return minutes + "分钟前";
        } else if (hours > 0 && hours < 24) {
            return hours + "小时前";
        } else {
            SimpleDateFormat format0 = new SimpleDateFormat("MM月dd日");
            return format0.format(time);
        }
    }

    /**
     * 计算运费的价格
     *
     * @param money
     * @return
     */
    public static int countFreightCost(int money) {
        int freightPrice = 450;

        if (money < 15000) {
            freightPrice = (int) Math.round(money * 0.03);
        }

        return freightPrice;
    }

    public static boolean judgeTimeIsGreaterThan(Date beforeTime, Integer hourNumber) {
        long currentTimeLong = new Date().getTime();
        long beforeTimeLong = beforeTime.getTime();

        long differTimeLong = hourNumber * 60 * 60 * 1000;

        if (currentTimeLong - beforeTimeLong >= differTimeLong) {
            return true;
        }
        return false;
    }

    /**
     * 字符串转换成小于等于20个字符
     *
     * @return
     */
    public static String stringToTransformLessThan(String str) {
        if (str ==null) {
            return "";
        }

        String[] strArray = str.split("");
        if (strArray.length <= 20) {
            return str;
        }

        return StringUtils.join(strArray, "", 0, 17) + "...";
    }
}
